//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    /*不用请求的数据*/
    // 进入后默认选中页面
    // tab_select: 0,

    /*之后请求的数据*/
    // 热点搜索
    hot_search: [
      "小零食",
      "小火锅",
      "小冰箱",
      "一加手机"
    ],
    // 顶部分类图标
    sort_img: "../../resources/images/icon/sort.png",
    // 顶部活动
    activity_img: "../../resources/images/icon/activity2.png",
    // banner图片
    banner: [
      "../../resources/images/banner/1.jpg",
      "../../resources/images/banner/2.jpg",
      "../../resources/images/banner/3.jpg",
      "../../resources/images/banner/4.jpg",
      "../../resources/images/banner/5.jpg"
    ],
    // 排行榜
    rangking_index: 0,
    ranking: [{
        name: "电脑整机",
        img: "../../resources/images/sort/1.jpg"
      },
      {
        name: "手机配件",
        img: "../../resources/images/sort/2.jpg"
      },
      {
        name: "家具家饰",
        img: "../../resources/images/sort/3.jpg"
      },
      {
        name: "图书文学",
        img: "../../resources/images/sort/5.png"
      }
    ],
    // sortList 分类
    sort: [{
      img: "../../resources/images/sort/1.jpg",
      name: "短袖T恤"
    }, {
      img: "../../resources/images/sort/2.jpg",
      name: "足球"
    }, {
      img: "../../resources/images/sort/3.jpg",
      name: "运动鞋"
    }, {
      img: "../../resources/images/sort/4.png",
      name: "中老年"
    }, {
      img: "../../resources/images/sort/5.png",
      name: "甜美风"
    }, {
      img: "../../resources/images/sort/6.jpg",
      name: "鱼尾裙"
    }, {
      img: "../../resources/images/sort/7.jpg",
      name: "相机配件"
    }, {
      img: "../../resources/images/sort/8.jpg",
      name: "护肤套装"
    }, {
      img: "../../resources/images/sort/9.jpg",
      name: "单肩包"
    }, {
      img: "../../resources/images/sort/10.jpg",
      name: "卫衣"
    }],
    // product_list 新品首发
    product_list: [{
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39000.jpg'
    }, {
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39001.jpg'
    }, {
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39002.jpg'
    }, {
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39003.jpg'
    }, {
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39004.jpg'
    }, {
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39005.jpg'
    }, {
      url: 'https://ossweb-img.qq.com/images/lol/web201310/skin/big39006.jpg'
    }],
    // recommend_list 热门推荐
    productList: [{
        id: 1077,
        img: "../../resources/images/product/1.jpg",
        name: "欧莱雅（LOREAL）奇焕光彩粉嫩透亮修颜霜 30ml（欧莱雅彩妆 BB霜 粉BB 遮瑕疵 隔离）",
        sale: 599,
        factory: 899,
        payNum: 2342
      },
      {
        id: 102377,
        img: "../../resources/images/product/2.jpg",
        name: "德国DMK进口牛奶  欧德堡（Oldenburger）超高温处理全脂纯牛奶1L*12盒",
        sale: 29,
        factory: 69,
        payNum: 999
      },
      {
        id: 10747,
        img: "../../resources/images/product/3.jpg",
        name: "【第2支1元】柔色尽情丝柔口红唇膏女士不易掉色保湿滋润防水 珊瑚红",
        sale: 299,
        factory: 699,
        payNum: 666
      },
      {
        id: 1054677,
        img: "../../resources/images/product/4.jpg",
        name: "百雀羚补水保湿护肤品",
        sale: 1599,
        factory: 2899,
        payNum: 236
      },
      {
        id: 1076787,
        img: "../../resources/images/product/5.jpg",
        name: "百草味 肉干肉脯 休闲零食 靖江精制猪肉脯200g/袋",
        sale: 599,
        factory: 899,
        payNum: 2399
      },
      {
        id: 107987,
        img: "../../resources/images/product/6.jpg",
        name: "短袖睡衣女夏季薄款休闲家居服短裤套装女可爱韩版清新学生两件套 短袖粉色长颈鹿 M码75-95斤",
        sale: 599,
        factory: 899,
        payNum: 2399
      },
      {
        id: 1053277,
        img: "../../resources/images/product/7.jpg",
        name: "多功能实用小书架 木架 易拆卸 进口原木",
        sale: 599,
        factory: 899,
        payNum: 2342
      },
      {
        id: 1075277,
        img: "../../resources/images/product/8.jpg",
        name: "时尚小圆桌旅行桌懒人桌",
        sale: 29,
        factory: 69,
        payNum: 999
      },
      {
        id: 1073167,
        img: "../../resources/images/product/10.jpg",
        name: "万宝路床上四件套",
        sale: 1599,
        factory: 2899,
        payNum: 236
      },
      {
        id: 1073267,
        img: "../../resources/images/product/9.jpg",
        name: "编不下去了 随便写点 将就看看就行 反正都是字",
        sale: 299,
        factory: 699,
        payNum: 666
      }
    ],
    // 当前数据项
    page: 1,
    // 是否允许请求数据
    pullUpOn: true,
    // test
    // change data item in an array
    // array: [{
    //   a: 777,
    //   b: 888
    // }, {
    //   c: 666,
    //   d: 999
    // }]
  },
  onLoad: function() {
    var that = this;

    console.log("进入index")
    // 自封装tabbar
    // var tabbar = app.globalData.tabbar,
    //   tab_color = app.globalData.tab_color;
    // that.setData({
    //   tabbar: tabbar,
    //   tab_color: tab_color
    // })rangking_index:0,

    // 排行榜自动切换
    var rangking_index = 0
    setInterval(function() {
      rangking_index = rangking_index + 1 < 4 ? rangking_index + 1 : 0;
      that.setData({
        rangking_index: rangking_index
      })
      // console.log(that.data.rangking_index)
    }, 3000);

    // 获取ASCll码中的大写字母
    // let list = [{}];
    // for (let i = 0; i < 26; i++) {
    //   list[i] = {};
    //   list[i].name = String.fromCharCode(65 + i);
    //   list[i].id = i;
    // }
    // console.log(list)

    if (app.globalData.UserInfo) {
      this.setData({
        userInfo: app.globalData.UserInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.UserInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  // 下拉刷新监听
  onPullDownRefresh: function() {
    console.log('开始刷新')
    var that = this;

    // test Change data item in an array
    // that.setData({
    //   'array[0].a': that.data.array[0].a + 1
    //   // setData引起的界面更新渲染完毕后的回调函数,两种写法
    //   // function(){}
    //   // ()=>{}
    // }, () => {
    //   console.log('已改变')
    // })
    // console.log(that.data.array)
    // wx.stopPullDownRefresh()
  },

  // 上拉触底监听
  onReachBottom: function() {
    console.log('触底刷新')
    var that = this,
      pageIndex = that.data.pageIndex,
      page = that.data.page;
    if (!that.data.pullUpOn) return;
    // 请求数据|请求数据项
    console.log(pageIndex)
    if (pageIndex) {
      // 请求数据
      if (pageIndex == page) {
        console.log(page + '满了')
        that.setData({
          pullUpOn: false
        })
      } else {
        let loadData = JSON.parse(JSON.stringify(this.data.productList));
        loadData = loadData.splice(0, 10)
        that.setData({
          productList: that.data.productList.concat(loadData),
          page: that.data.page + 1,
        })
      }
    } else {
      // 请求数据项
      that.setData({
        pageIndex: 3
      })
      // 数据绑定
      that.setData({
        productList: that.data.productList.concat(that.data.productList),
        page: that.data.page + 1,
      })
    }
  },
  //Demo事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  search: function(e) {
    var that = this;

    // 在非当前页更改当前页的数据Test
    // that.setData({
    //   res: 22
    // })

    var search_name = e.currentTarget.dataset.name
    var url = '../search/search'
    //  + '?name=' + search_name
    console.log(url)

    wx.navigateTo({
      url: url,
      events: {
        // 页面回调监听,可以开任意个
        // test: function(data) {
        //   console.log(data)
        //   console.log(that.data.res)
        // }
      },
      success: function(res) {
        // 传送数据给打开的页面
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: search_name
        })
      }
    })
  },
  goods_info: function(e) {
    var that = this;
    console.log(e);
    var goods_id = e.currentTarget.dataset.id
    var url = '../goods/goods'
    wx.navigateTo({
      url: url + "?goods_id=" + goods_id
    })
  },
  onShow: function(e) {},
















  // 切换tabbar
  // tab_switch: function(e) {
  //   var that = this;
  //   let index = e.currentTarget.dataset.index;
  //   that.setData({
  //     tab_select: index
  //   })
  //   console.log(index)
  //   switch (index) {
  //     case 0:
  //       wx.redirectTo({
  //         url: '../index/index',
  //       })
  //       break;
  //     case 1:
  //       wx.redirectTo({
  //         url: '../sort/sort',
  //       })
  //       break;
  //     case 2:
  //       wx.redirectTo({
  //         url: '../cart/cart',
  //       })
  //       break;
  //     case 3:
  //       wx.redirectTo({
  //         url: '../my/my',
  //       })
  //       break;
  //   }
  // },
})